package com.skyon.sdk;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
import com.skyon.sdk.bean.ResultBean;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class MSUActivityLifecycle implements Application.ActivityLifecycleCallbacks {
    private static long start = 0;
    private String view = "";

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        MSUCommon.viewStack.push(activity.getClass().getSimpleName());
    }

    @Override
    public void onActivityStarted(Activity activity) {
    }

    @Override
    public void onActivityResumed(final Activity context) {
        start = System.currentTimeMillis();
        view = context.getClass().getSimpleName();
        final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss", Locale.CHINA);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (!SkyonSdk.isIsCollectD()) {
                        Thread.sleep(3000);
                    }
                } catch (InterruptedException e) {
                }
                final ResultBean mResultBean = new ResultBean();
                mResultBean.setModel1(MSUCommon.model1);
                mResultBean.setIMEI20(MSUCommon.getIMEI20(context));
                mResultBean.setDeviceId22(MSUCommon.getDeviceId22(context));
                mResultBean.setCurCpuFreq38(MSUCommon.getCurCpuFreq38());
                mResultBean.setStatus39(MSUCommon.resultBean.getStatus39());
                mResultBean.setLevel40(MSUCommon.resultBean.getLevel40());
                mResultBean.setScale41(MSUCommon.resultBean.getScale41());
                mResultBean.setTemperature42(MSUCommon.resultBean.getTemperature42());
                mResultBean.setVersionCode43(MSUCommon.getVersionCode43(context));
                mResultBean.setKey44(MSUCommon.getKey44(context));
                mResultBean.setAppName45(MSUCommon.getAppName45(context));
                mResultBean.setAppProcessName46(MSUCommon.getAppProcessName46(context));
                mResultBean.setUserID47(MSUCommon.getUserID47(context));
                mResultBean.setViewPage58(view);
                mResultBean.setViewVisitTime59(formatter.format(start));
                mResultBean.setCollectEventType62("5");
                mResultBean.setCollectType63("页面加载事件触发采集");
                mResultBean.setAppId64(MSUCommon.getAppId64(context));
                MSUHttpUtils.mPost(mResultBean.toString());
                Log.d("SkyonSdk:", "collectionOnLoad......" + mResultBean.toString());
            }
        }).start();

    }

    @Override
    public void onActivityPaused(final Activity context) {
        ResultBean mResultBean = null;
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss", Locale.CHINA);
        long end = System.currentTimeMillis();
        long time = System.currentTimeMillis() - start;
        if (context.getClass().getSimpleName().equals(view)) {
            mResultBean = new ResultBean();
            mResultBean.setModel1(MSUCommon.model1);
            mResultBean.setIMEI20(MSUCommon.getIMEI20(context));
            mResultBean.setDeviceId22(MSUCommon.getDeviceId22(context));
            mResultBean.setCurCpuFreq38(MSUCommon.getCurCpuFreq38());
            mResultBean.setStatus39(MSUCommon.resultBean.getStatus39());
            mResultBean.setLevel40(MSUCommon.resultBean.getLevel40());
            mResultBean.setScale41(MSUCommon.resultBean.getScale41());
            mResultBean.setTemperature42(MSUCommon.resultBean.getTemperature42());
            mResultBean.setVersionCode43(MSUCommon.getVersionCode43(context));
            mResultBean.setKey44(MSUCommon.getKey44(context));
            mResultBean.setAppName45(MSUCommon.getAppName45(context));
            mResultBean.setAppProcessName46(MSUCommon.getAppProcessName46(context));
            mResultBean.setUserID47(MSUCommon.getUserID47(context));
            mResultBean.setViewPage58(view);
            mResultBean.setViewVisitTime59(formatter.format(start));
            mResultBean.setViewGoneTime60(formatter.format(end));
            mResultBean.setViewStayTime61(String.valueOf(time));
            mResultBean.setCollectEventType62("6");
            mResultBean.setCollectType63("页面离开事件触发采集");
            mResultBean.setAppId64(MSUCommon.getAppId64(context));
        }
        final ResultBean finalMResultBean = mResultBean;
        new Thread(new Runnable() {
            @Override
            public void run() {
                MSUHttpUtils.mPost(finalMResultBean.toString());
                Log.d("SkyonSdk:", "collectionOnJump......" + finalMResultBean.toString());
            }
        }).start();

    }

    @Override
    public void onActivityStopped(Activity activity) {
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
}